package com.pactera.asmp.server.service;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.pactera.asmp.server.common.constants.ResultCode;
import com.pactera.asmp.server.dao.CameraInfoMapper;
import com.pactera.asmp.server.entity.CameraInfo;
import com.pactera.asmp.server.entity.CameraInfoDTO;
import com.pactera.asmp.server.entity.CameraInfoExample;
import com.pactera.asmp.server.entity.ResponseResult;

@Service
public class CameraServiceImpl implements ICameraService{

    @Autowired
    private CameraInfoMapper cameraInfoMapper;

    @Override
    public ResponseResult registerCamera(CameraInfoDTO cameraInfoDTO) {
        ResponseResult responseResult;
        if (null == cameraInfoDTO) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtils.isEmpty(cameraInfoDTO.getCuid())) {
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        CameraInfoExample example = new CameraInfoExample();
        example.createCriteria().andCuidEqualTo(cameraInfoDTO.getCuid());
        List<CameraInfo> cameraInfos = cameraInfoMapper.selectByExample(example);
        CameraInfo cameraInfo;
        if(CollectionUtils.isNotEmpty(cameraInfos)) {
            cameraInfo = cameraInfos.get(0);
            cameraInfo.setSn(cameraInfoDTO.getSn());
            cameraInfo.setName(cameraInfoDTO.getName());
            cameraInfo.setModel(cameraInfoDTO.getModel());
            cameraInfo.setBrand(cameraInfoDTO.getBrand());
            cameraInfo.setVersionCode(cameraInfoDTO.getVersionCode());
            cameraInfo.setNickName(cameraInfoDTO.getNickName());
            cameraInfo.setStatus(0);
            cameraInfoMapper.updateByPrimaryKey(cameraInfo);
        }else {
            cameraInfo = new CameraInfo();
            BeanUtils.copyProperties(cameraInfoDTO, cameraInfo);
            cameraInfo.setStatus(0);
            cameraInfo.setRegisterTime(LocalDateTime.now());
            cameraInfoMapper.insert(cameraInfo);
        }
        return ResponseResult.defaultSuccess(null);
    }

    @Override
    public ResponseResult offlineCamera(CameraInfoDTO cameraInfoDTO) {
        if (null == cameraInfoDTO) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtils.isEmpty(cameraInfoDTO.getCuid())) {
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        CameraInfoExample example = new CameraInfoExample();
        example.createCriteria().andCuidEqualTo(cameraInfoDTO.getCuid());
        List<CameraInfo> cameraInfos = cameraInfoMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(cameraInfos)) {
            return new ResponseResult(ResultCode.BUSINESS_CAMERA_NOT_EXISTED);
        }
        CameraInfo cameraInfo = cameraInfos.get(0);
        cameraInfo.setStatus(2);
        cameraInfo.setDisconnectTime(LocalDateTime.now());
        this.update(cameraInfo);
        return ResponseResult.defaultSuccess(null);
    }

    @Override
    public ResponseResult occupyCamera(CameraInfoDTO cameraInfoDTO) {
        if (null == cameraInfoDTO) {
            return new ResponseResult(ResultCode.PARAM_IS_BLANK);
        }
        if (StringUtils.isEmpty(cameraInfoDTO.getCuid()) || cameraInfoDTO.getUserId() == null) {
            return new ResponseResult(ResultCode.PARAM_NOT_COMPLETE);
        }
        CameraInfoExample example = new CameraInfoExample();
        example.createCriteria().andCuidEqualTo(cameraInfoDTO.getCuid());
        List<CameraInfo> cameraInfos = cameraInfoMapper.selectByExample(example);

        if(CollectionUtils.isEmpty(cameraInfos)) {
            return new ResponseResult(ResultCode.BUSINESS_CAMERA_NOT_EXISTED);
        }
        CameraInfo cameraInfo = cameraInfos.get(0);
        cameraInfo.setStatus(1);
        cameraInfo.setUserId(cameraInfoDTO.getUserId());
        this.update(cameraInfo);
        return ResponseResult.defaultSuccess(null);
    }

    private int update (CameraInfo cameraInfo) {
        return cameraInfoMapper.updateByPrimaryKeySelective(cameraInfo);
    }

    @Override
    public List<CameraInfo> select(Integer labId) {
        CameraInfoExample example = new CameraInfoExample();
        CameraInfoExample.Criteria criteria = example.createCriteria();
        if(labId != null) {
            criteria.andLabIdEqualTo(labId);
        }
        return cameraInfoMapper.selectByExample(example);
    }

    @Override
    public ResponseResult inOccupyCamera(CameraInfoDTO cameraInfoDTO) {
        if (null != cameraInfoDTO && StringUtils.isEmpty(cameraInfoDTO.getCuid())) {
            CameraInfoExample example = new CameraInfoExample();
            example.createCriteria().andCuidEqualTo(cameraInfoDTO.getCuid()).andUserIdEqualTo(cameraInfoDTO.getUserId()).andStatusEqualTo(1);
            List<CameraInfo> cameraInfos = cameraInfoMapper.selectByExample(example);

            if(CollectionUtils.isNotEmpty(cameraInfos)) {
                CameraInfo cameraInfo = cameraInfos.get(0);
                cameraInfo.setStatus(0);
                cameraInfo.setUserId(cameraInfoDTO.getUserId());
                this.update(cameraInfo);
            }
        }
        return ResponseResult.defaultSuccess(null);
    }
}
